package com.example.demo.Control;

import com.example.demo.Common.AjaxResult;
import com.example.demo.Common.Session;
import com.example.demo.Entity.Friend;
import com.example.demo.Entity.MessageEntity;
import com.example.demo.Entity.MessageSession;
import com.example.demo.Entity.Userinfo;
import com.example.demo.Service.MessageService;
import com.example.demo.Service.MessageSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/session")
public class MessageSessionControl {
    @Autowired
    public MessageSessionService messageSessionService;
    @Autowired
    public MessageService messageService;
    @RequestMapping("/getList")
    public AjaxResult getSessionUserid(HttpServletRequest req){
        HttpSession session= req.getSession(false);
        if(session==null){
            return AjaxResult.fail(-1,"用户未有登录");
        }
        Userinfo userinfo=(Userinfo) session.getAttribute(Session.SEESSINO_KEY);
        if(userinfo==null){
            return AjaxResult.fail(-1,"该用户不存在");
        }
        System.out.println(userinfo.getUserid());
        List<Integer> list=messageSessionService.getSessionId(userinfo.getUserid());
        List<MessageSession> MessageSessionList=new ArrayList<>();
        for (Integer item:list) {
            MessageSession session1=new MessageSession();
            List<Friend> arr = messageSessionService.getUserid(item,userinfo.getUserid());
            session1.setSessionId(item);
            session1.setFriends(arr);
            MessageEntity messageEntity=messageService.getLastMessage(item);
            if(messageEntity==null){
                session1.setLastMessage(" ");
            }else{
                session1.setLastMessage(messageEntity.getContent());
            }
            MessageSessionList.add(session1);
        }
        return AjaxResult.success(MessageSessionList);
    }
    @RequestMapping("/addSession")
    @Transactional
    public AjaxResult addSession(HttpServletRequest req,int userid){
        HttpSession session=req.getSession(false);
        Userinfo userinfo=(Userinfo) session.getAttribute(Session.SEESSINO_KEY);
        if(session==null){
            return AjaxResult.fail(-1,"用户未登录");
        }
        if(userinfo==null){
            return  AjaxResult.fail(-1,"该用户不存在");
        }
        MessageSession messageSession=new MessageSession();
        messageSessionService.addSession(messageSession);
        int sessionId= messageSession.getSessionId();
        messageSessionService.addSessionUser(sessionId,userinfo.getUserid());
        messageSessionService.addSessionUser(sessionId,userid);
       return AjaxResult.success(sessionId);
    }
}
